using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using pos.entity;
using NHibernate.Criterion;
using System.Collections;
namespace idea.tools
{
    class CommodityComboxTool
    {
        private ComboBox cboList;

        public bool active
        {
            get
            {
                if (cboList == null)
                    return false;
                return cboList.Visible;
            }
           
        }
        private Control txtCmd;
        public delegate void _OnClose(TNameValue para);
        public _OnClose onClose;
        public CommodityComboxTool(Form parent, Control position, _OnClose onclose)
        {
            
            if (cboList == null)
            {
                cboList = new ComboBox();

                cboList.AutoSize = true;
                cboList.Font = position.Font;
                cboList.Location = position.Location;
                cboList.DropDownHeight = parent.Height-100;
               
                cboList.Name = "cboList";
                cboList.Size = position.Size;
                cboList.Width = 700;
                cboList.TabIndex = 0;
                

                parent.Controls.Add(cboList);
                txtCmd = position;
                onClose = onclose;
                
            }

            cboList.SelectionChangeCommitted += cboList_SelectionChangeCommitted;
            cboList.DropDownClosed += cboList_DropDownClosed;
            cboList.KeyDown += cboList_KeyDown;
            
        }
        public void showList(string key, Control position)
        {
            cboList.Location = position.Location;
            showList(key);

        }
        public  void showList(string key)
        {
            cboList.Items.Clear();
         
            cboList.Visible = true;
            
            //String sql;
            //sql = string.Format("where commodityID like '%{0}%'", key);
            t_commodity t = new t_commodity();
            DetachedCriteria dc; 
            
            try
            {
                Convert.ToInt64(key);
               dc=t.DC.Add(Restrictions.Like("commodityID", "%" + key + "%"));
            }
            catch
            {
               // sql = string.Format("where commodityKey like '%{0}%'", key);
                dc = t.DC.Add(Restrictions.Like("commodityKey", "%" + key + "%"));
            }
            dc.AddOrder(Order.Asc("commodityName"));
            IList  list = t.List(dc);
           // DataSet set = t.query(sql, "order by commodityName");
            //   DataSet set = t.query(string.Format("where commodityKey like '%{0}%'", key), "order by seqID");
            //List<TNameValue> l1 = new List<TNameValue>();
            cboList.DisplayMember = "Name";
            cboList.ValueMember = "Value";
            foreach (Commodity r in list)
            {
                t.Init(r);
                //cboList.Items.Add(t.commodityName);

                TNameValueEx vo1 = new TNameValueEx();
                vo1.Name = string.Format("{0,-15}{2}", t.commodityID,t.retailPrice,t.commodityName);
                vo1.Value = t.commodityID;
                vo1.Key = t.commodityName;
                cboList.Items.Add(vo1);
            }
            TNameValueEx vo = new TNameValueEx();
            vo.Name = "";
            vo.Value = "";
            vo.Key = "";
            cboList.Items.Add(vo);
            cboList.Focus();
            cboList.DroppedDown = true;
          
        }
        private void cboList_DropDownClosed(object sender, EventArgs e)
        {
            cboList.Visible = false;
        }
        private void cboList_SelectionChangeCommitted(object sender, EventArgs e)
        {
            
            if (cboList.SelectedItem == null)
                return;
            TNameValueEx t = (TNameValueEx)cboList.SelectedItem;
            //txtCmd.Text = t.Value;
            //  RowAddOne();
            cboList.Items.Clear();
            TNameValue para = new TNameValue(t.Key,t.Value);
            
            onClose(para);
        }
        private void cboList_KeyDown(object sender, KeyEventArgs e)
        {

            switch (e.KeyCode)
            {
                case Keys.Enter:
                    {
                        if (cboList.Visible == false)
                        {
                            
                            return;
                        }
                        cboList.Visible = false;

                        //   cboList.Parent.Focus();
                        if (cboList.SelectedItem == null)
                            return;
                        TNameValueEx t = (TNameValueEx)cboList.SelectedItem;
                        //      txtCmd.Text = t.Value;
                        //  RowAddOne();
                        cboList.Items.Clear();
                        TNameValue para = new TNameValue(t.Key, t.Value);

                        onClose(para);
                        break;
                    }
             
            }
            
            
           
            
        }
    }
}
